package com.qf.listener;

import com.qf.service.IGoodsService;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class MQListener {

    @Autowired
    private IGoodsService goodsService;

    //@Queue(name = "goods_queue", durable = "true")
    //@Exchange(name = "seckill_exchange", type="fanout", declare="true")
    //@QueueBinding
    @RabbitListener(bindings =
            @QueueBinding(value = @Queue(name = "goods_queue", durable = "true"),
            exchange = @Exchange(name = "seckill_exchange", type="fanout", declare="true")))
    public void msgHandler(Map<String, Object> msg){
        System.out.println("商品服务接收到消息：" + msg);
        //商品id
        Integer gid = (Integer) msg.get("gid");
        //用户id
        Integer uid = (Integer) msg.get("uid");
        //下单数量
        Integer gnumber = (Integer) msg.get("gnumber");

        //扣除商品的库存
        goodsService.updateGoodsSave(gid, gnumber);
    }
}
